package com.programming.forum.services.impl;

import com.programming.forum.common.AppResult;
import com.programming.forum.common.ResultCode;
import com.programming.forum.dao.ArticleMapper;
import com.programming.forum.dao.ArticleReplyMapper;
import com.programming.forum.exception.ApplicationException;
import com.programming.forum.model.Article;
import com.programming.forum.model.ArticleReply;
import com.programming.forum.services.IArticleReplyService;
import com.programming.forum.services.IArticleService;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 22479
 * Date: 2023-11-12
 * Time: 17:43
 */
@Service
@Slf4j
public class ArticleReplyImpl implements IArticleReplyService {

    @Resource
    private ArticleReplyMapper articleReplyMapper;
    @Resource
    private IArticleService articleService;
    @Resource
    private ArticleMapper articleMapper;


    /**
     * 新增帖子回复
     * @param articleReply
     */
    @Override
    public void create(ArticleReply articleReply) {
        //非空校验
        if(articleReply == null || articleReply.getArticleId() == null || articleReply.getPostUserId() == null
        || articleReply.getArticleId() == null) {
            //FAILED_PARAMS_VALIDATE  (1002, "参数校验失败"),
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());

            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        //设置默认值
        articleReply.setReplyId(null);
        articleReply.setReplyUserId(null);
        articleReply.setState((byte) 0);
        articleReply.setDeleteState((byte) 0);
        Date date = new Date();
        articleReply.setCreateTime(date);
        articleReply.setUpdateTime(date);
        //写入数据库
        int row = articleReplyMapper.insertSelective(articleReply);
        if(row != 1) {
            //  FAILED                  (1000, "操作失败"),
            log.warn(ResultCode.FAILED .toString() + "更新数据库异常");

            throw new ApplicationException(AppResult.failed(ResultCode.FAILED ));
        }
        //更新帖子回复数
        articleService.addOneReplyCountById(articleReply.getArticleId());
        //打印日志
        log.info("回复成功 userId =" + articleReply.getPostUserId());
    }

    @Override
    /**
     * 根据帖子Id查询所有回复
     * @param id 帖子Id
     * @return
     */
    public List<ArticleReply> selectByArticleId( Long articleId) {
        //非空校验
        if(articleId == null || articleId <= 0) {
            //FAILED_PARAMS_VALIDATE  (1002, "参数校验失败"),
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());

            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        // 获取帖子详情
        Article article = articleMapper.selectByPrimaryKey(articleId);
        // 帖子不存在
        if (article == null || article.getDeleteState() == 1) {
            // 打印日志  FAILED_NOT_EXISTS        (1005, "资源不存在"),
            log.warn(ResultCode. FAILED_NOT_EXISTS .toString());
            // 抛出异常
            throw new ApplicationException(AppResult.failed(ResultCode. FAILED_NOT_EXISTS ));
        }
        // 帖子状态异常
        if (article.getState() == 1) {
            // 打印日志  FAILED_FORBIDDEN        (1003, "禁止访问"),
            log.warn(ResultCode.FAILED_FORBIDDEN .toString());
            // 抛出异常
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_FORBIDDEN ));
        }
        //调用dao
        List<ArticleReply> result = articleReplyMapper.selectByArticleId(articleId);
        return result;

    }
}
